import { ObjectTool } from '@es-tool/core'
import { EmptyResponseError } from '../exception/EmptyResponseError'
import { ResponseStatusException } from '../exception/ResponseStatusException'

// noinspection JSUnusedGlobalSymbols
/**
 * ListResponse
 * @author LL
 * @date 2022-01-20 上午 10:41
 **/
export class ListResponse<T> {
    public limit?: number
    public error?: string
    public code?: string | number
    public ok?: boolean
    public data: T[]
    public offset?: number
    public total?: number
    public requestId?: string

    public static getData<T>(res: ListResponse<T>): T[] {
        if (ObjectTool.isEmpty(res)) {
            throw new EmptyResponseError()
        }
        if (res.ok) {
            return res.data
        }
        throw new ResponseStatusException<ListResponse<T>>(res)
    }
}
